package com.dgut.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

public class AppResult<T> {

    @JsonInclude(JsonInclude.Include.ALWAYS)//任何时候都要参与序列化,无论是否为空
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS)//任何时候都要参与序列化,无论是否为空
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS)//任何时候都要参与序列化,无论是否为空
    private T data;
    public AppResult(int code, String message) {
        this(code,message,null);
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    /*
    * 成功
    * */
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }

    public static <T> AppResult<T> success(T data){
        return new AppResult (ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(),message);
    }
    public static AppResult success(int code,String message){
        return new AppResult(code,message);
    }
    public static <T> AppResult<T> success(int code,String message,T data){
        return new AppResult(code,message,data);
    }

    /*
    * 失败
    * */
    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    public static <T> AppResult<T> failed(T data){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(),data);
    }
    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }
    public static <T> AppResult<T> failed(int code){
        return new AppResult(code, ResultCode.FAILED.getMessage());
    }
    public static AppResult failed(ResultCode resultCode){
        return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }
    public static <T> AppResult<T> failed(ResultCode resultCode,T data){
        return new AppResult(resultCode.getCode(), resultCode.getMessage(),data);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
